package studio.raptor.dispatcher.common.util;

/**
 * @author jack
 * @since 1.0
 */
public class StringUtil {

  private static final char PACKAGE_SEPARATOR_CHAR = '.';

  /**
   * The shortcut to {@link #simpleClassName(Class) simpleClassName(o.getClass())}.
   */
  public static String simpleClassName(Object o) {
    if (o == null) {
      return "null_object";
    } else {
      return simpleClassName(o.getClass());
    }
  }

  /**
   * Generates a simplified name from a {@link Class}.  Similar to {@link Class#getSimpleName()}, but it works fine
   * with anonymous classes.
   */
  public static String simpleClassName(Class<?> clazz) {
    String className = ObjectUtil.checkNotNull(clazz, "clazz").getName();
    final int lastDotIdx = className.lastIndexOf(PACKAGE_SEPARATOR_CHAR);
    if (lastDotIdx > -1) {
      return className.substring(lastDotIdx + 1);
    }
    return className;
  }

}
